package com.lmmob.autotest.uitest;

import cucumber.annotation.After;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;



import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

public class WebDriverFacade {

    @SuppressWarnings("unchecked")
    private static Constructor<WebDriver> getDriverConstructor(String driver) {
        try {

            return (Constructor<WebDriver>) Thread.currentThread().getContextClassLoader().loadClass(driver).getConstructor(Capabilities.class);
        } catch (Throwable problem) {
            problem.printStackTrace();
            throw new RuntimeException("Couldn't load " + driver, problem);
        }
    }

    private static WebDriver browser;

    public static WebDriver getWebDriver(String driver, String browserName, Platform platform) throws InvocationTargetException, IllegalAccessException, InstantiationException, MalformedURLException {
        Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    if (browser != null) {
                    	try {
							Thread.sleep(Constants.SLEEP_TIME);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                        browser.close();
                        browser.quit();
                    }
                }
            });
        System.out.println("--------------------------------------------------------||||||||||||||||||||||||||||"+browser);
        if (browser == null) {
//            Constructor<WebDriver> webDriverConstructor = getDriverConstructor(driver);
            DesiredCapabilities capabilities = new DesiredCapabilities(browserName, "", platform);
            if (browserName.equals("internet explorer")) {
                //capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
                //capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                
            	capabilities = DesiredCapabilities.internetExplorer(); 
            	capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
                capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, false); 
				
            } else if (browserName.equals("firefox")) {
            	
                FirefoxProfile profile = new FirefoxProfile();
                profile.setEnableNativeEvents(true);
                //try {
                    //profile.addExtension(WebDriverFacade.class, "firebug-1.9.1-fx.xpi");
                //} catch (IOException e) {

                //}
                //profile.setPreference("extensions.firebug.currentVersion", "1.9.1");
                capabilities.setCapability(FirefoxDriver.PROFILE, profile);
                
            }
            FirefoxProfile profile = new FirefoxProfile();
            profile.setEnableNativeEvents(true);
           // browser = new InternetExplorerDriver(capabilities);
          browser=new FirefoxDriver(profile);
            //browser = new RemoteWebDriver(new URL("http://192.168.102.60:4444/wd/hub"), capabilities);
            browser.manage().window().maximize();
        }
        System.out.println("--------------------------------------------------------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+browser);
        return browser;
    }


    @After
    public void afterAScenario() {
        if (browser != null) {
            browser.manage().deleteAllCookies();
        }
    }
}